<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <ul id="list">
        <li id="li1">列表项1</li>
    </ul>
    <button id="btn">添加新节点</button>
    <button id="btn1">向已有节点前添加新节点</button>
</body>
<script>
    var list = document.getElementById("list");
    var li1 = document.getElementById("li1");
    var btn = document.getElementById("btn");
    var btn1 = document.getElementById("btn1");
    var index=2,j=0;
    btn.onclick = function(){
        var liNode = document.createElement("li");
        liNode.innerHTML = "<a href='#'>列表项" + index + "</a>";
        index = index + 1;
        list.appendChild(liNode);
    }
    btn1.onclick =function(){
        var liNode = document.createElement("li");
        liNode.innerHTML = "<a href='#'>列表项3</a>";
        list.insertBefore(liNode,li1);
    }
</script>
</html>